﻿using DAL.comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class InStockPurchaseBillDAL
    {
        private SqlCommand cmd;
        public InStockPurchaseBillDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
        }
        /// <summary>
        /// 获取待审核订单列表
        /// </summary>
        public List<string> GetInStockPurchaseBill()
        {
            cmd.CommandText = "GetInStockPurchaseBill";
            cmd.Parameters.Clear();
            List<string> list = new List<string>();
            SqlDataReader reader = SQLHelper.ExcuteReader(cmd);//读数据库
            while (reader.Read())
            {
                string id = reader["PurchaseID"].ToString();
                list.Add(id);
            }
            reader.Close();//关闭读库
            return list;
        }
        public bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 审核并同意入库
        /// </summary>
        public bool AgreeInStock(PurchaseBill bo)
        {
            //SqlTransaction transaction = SQLHelper.ExcuteSqlTransaction(cmd);
            //try
            //{
                cmd.CommandText = "AgreeInStock";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;
                cmd.Parameters.Add("@Custodian", SqlDbType.NVarChar, 6).Value = bo.Custodian;
                cmd.Parameters.Add("Memo", SqlDbType.NVarChar, 100).Value = bo.Memo;

            //    int num = cmd.ExecuteNonQuery();
            //    if (num > 0)
            //    {
            //        transaction.Commit();
            //        return true;
            //    }
            //    else
            //        return false;
            //}
            //catch//(Exception ex)
            //{
            //    transaction.Rollback();//回滚
            //    return false;
            //}
            //finally
            //{
            //    SQLHelper.CloseConn();//关闭DB连接
            //}
            return ForEdit(cmd );
        }
        /// <summary>
        /// 不同意入库
        /// </summary>
        public bool NotAgreeInStock(PurchaseBill bo)
        {
            //SqlTransaction transaction = SQLHelper.ExcuteSqlTransaction(cmd);
            //try
            //{
                cmd.CommandText = "NotAgreeInStock";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;
                cmd.Parameters.Add("Custodian", SqlDbType.NVarChar, 6).Value = bo.Custodian;
                cmd.Parameters.Add("Memo", SqlDbType.NVarChar, 100).Value = bo.Memo;

            //    int num = cmd.ExecuteNonQuery();
            //    if (num > 0)
            //    {
            //        transaction.Commit();
            //        return true;
            //    }
            //    else
            //        return false;
            //}
            //catch//(Exception ex)
            //{
            //    transaction.Rollback();//回滚
            //    return false;
            //}
            //finally
            //{
            //    SQLHelper.CloseConn();//关闭DB连接
            //}
            return ForEdit(cmd);
        }

    }
}
